﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class KhachHang : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";
        int idThem = 23;
        int idSua = 24;
        int idXoa = 25;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION) && frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);

            }
        }
        public KhachHang()
        {
            InitializeComponent();

        }
        private void RefreshForm(string p)
        {
            txtMa.Text = GetCusNextNo();
            txtTen.Text = p;
            txtDaiDien.Text = p;
            txtDiaChi.Text = p;
            txtDienThoai.Text = p;
            txtFax.Text = p;
            txtHanMucNo.Text = p;
            txtMaSoThue.Text = p;
            txtNganHang.Text = p;
            txtTaiKhoan.Text = p;
            txtTinhTP.Text = p;
        }

        private string GetCusNextNo()
        {
            BUS.CommonControl ctr = new BUS.CommonControl();
            return ctr.GetCusNextNo(rType.SelectedIndex == 0 ? "KH" : "NC");
        }
        private DTO.CUSTINFOInfo GetData()
        {
            double hanmuc = txtHanMucNo.EditValue != null ? Convert.ToDouble(txtHanMucNo.EditValue) : 0;
            return new DTO.CUSTINFOInfo(txtMa.Text, txtTen.Text, txtDaiDien.Text, txtDiaChi.Text, txtDienThoai.Text, txtFax.Text, txtMaSoThue.Text, rType.EditValue.ToString() == "Supplier", txtTaiKhoan.Text, txtNganHang.Text, txtTinhTP.Text, hanmuc, !ckbLock.Checked);
        }
        private void EnabledForm(bool p)
        {
            txtMa.Focus();
            txtMa.Properties.ReadOnly = !p;
            txtTen.Properties.ReadOnly = !p;
            txtDaiDien.Properties.ReadOnly = !p;
            txtDiaChi.Properties.ReadOnly = !p;
            txtDienThoai.Properties.ReadOnly = !p;
            txtFax.Properties.ReadOnly = !p;
            txtHanMucNo.Properties.ReadOnly = !p;
            txtMaSoThue.Properties.ReadOnly = !p;
            txtNganHang.Properties.ReadOnly = !p;
            txtTaiKhoan.Properties.ReadOnly = !p;
            txtTinhTP.Properties.ReadOnly = !p;
            btnXoa.Enabled = !p;
            btnThem.Enabled = !p;
            btnLuu.Enabled = p;
            btnXoa.Enabled = !p;
            btnSua.Enabled = !p;
            btnBoQua.Enabled = p;
            gridKhachHang.Enabled = !p;
            rType.Enabled = !p;
            ckbLock.Enabled = !p;
        }
        private void SetData(DTO.CUSTINFOInfo inf)
        {
            txtMa.Text = inf.CUSTID;
            txtTen.Text = inf.TENGD;
            txtDaiDien.Text = inf.DAIDIEN;
            txtDiaChi.Text = inf.DIACHI;
            txtDienThoai.Text = inf.DIENTHOAI;
            txtFax.Text = inf.FAX;
            txtHanMucNo.EditValue = inf.HMDUNO;
            txtMaSoThue.Text = inf.MSTHUE;
            txtNganHang.Text = inf.NGANHANG;
            txtTaiKhoan.Text = inf.TAIKHOAN;
            txtTinhTP.Text = inf.TINH_TP;
            ckbType.Checked = inf.NHACUNGCAP;
        }

        private void KhachHang_Load(object sender, EventArgs e)
        {
            EnabledForm(false);
            rType.SelectedIndex = 0;
            LoadKhachHang();
            SetPermission();
        }

        private void LoadKhachHang()
        {
            BUS.CUSTINFOControl ctr = new BUS.CUSTINFOControl();
            gridKhachHang.DataSource = ctr.GetAll(rType.EditValue.ToString() == "Supplier", !ckbLock.Checked, ref sErr);
        }

        private void rType_EditValueChanged(object sender, EventArgs e)
        {
            LoadKhachHang();
        }

        private void ckbLock_CheckedChanged(object sender, EventArgs e)
        {
            LoadKhachHang();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefreshForm("");
            EnabledForm(true);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
            txtMa.Properties.ReadOnly = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (txtMa.Text == "")
            {
                XtraMessageBox.Show("Vui lòng nhập mã!");
                return;
            }
            DTO.CUSTINFOInfo inf = GetData();
            BUS.CUSTINFOControl ctr = new BUS.CUSTINFOControl();
            if (txtMa.Properties.ReadOnly == false)
            {
                if (ctr.IsExist(txtMa.Text))
                {
                    XtraMessageBox.Show("Mã khách hàng đã tồn tại, vui lòng thay thé mã khách hàng khác!");
                    return;
                }
            }
            sErr = ctr.InsertUpdate(inf);
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show(sErr, "Lỗi");
            gridKhachHang.DataSource = ctr.GetAll(ref sErr);
            EnabledForm(false);
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (gridKhachHang.CurrentRow != null)
            {
                BUS.CUSTINFOControl ctr = new BUS.CUSTINFOControl();
                DTO.CUSTINFOInfo inf = new DTO.CUSTINFOInfo((DataRowView)gridKhachHang.CurrentRow.DataRow);
                if (!ctr.IsUsed(inf.CUSTID))
                {
                    if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        sErr = ctr.Delete(inf.CUSTID);
                        if (sErr == "")
                            XtraMessageBox.Show("Xóa thành công", "Thông báo");
                        else XtraMessageBox.Show(sErr, "Lỗi");
                        gridKhachHang.DataSource = ctr.GetAll(ref sErr);
                    }
                }
                else
                    if (XtraMessageBox.Show("Khách hàng đã được dùng, bạn có muốn khóa khách hàng?", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        inf.TRANSLOCK = true;
                        sErr = ctr.InsertUpdate(inf);
                        if (sErr == "")
                            XtraMessageBox.Show("Khóa thành công", "Thông báo");
                        else XtraMessageBox.Show(sErr, "Lỗi");
                        gridKhachHang.DataSource = ctr.GetAll(ref sErr);
                    }
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void KhachHang_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
            }
          
        }

        private void gridKhachHang_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnSua.Enabled)
                    btnSua_Click(null, null);
            }
            else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
            {
                if (btnThem.Enabled)
                    btnThem_Click(null, null);
            }
            else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
            {
                if (btnXoa.Enabled)
                    btnXoa_Click(null, null);
            }
        }

        private void gridKhachHang_RowEditCanceled(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {

        }

        private void gridKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            if (gridKhachHang.CurrentRow != null && gridKhachHang.CurrentRow.DataRow is DataRowView)
            {
                DTO.CUSTINFOInfo inf = new DTO.CUSTINFOInfo((DataRowView)gridKhachHang.CurrentRow.DataRow);
                SetData(inf);
            }
        }


    }
}